//
//  WBMainTabBarController.swift
//  Weibo
//
//  Created by 张俭 on 16/8/17.
//  Copyright © 2016年 张俭. All rights reserved.
//

import UIKit

class WBMainTabBarController: UITabBarController {
    fileprivate lazy var composeButton : UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: [])
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: [])
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        button.sizeToFit()
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        setupComposeButton()
    }
    
    @objc fileprivate func compose(){
        let composeTypeView = WBComposeTypeView.composeTypeView()
        
        composeTypeView.show()
        
    }
    
}

// MARK: - 初始化发布按钮
private extension WBMainTabBarController {
    func setupComposeButton() {
        tabBar.addSubview(composeButton)
        
        let count = CGFloat(childViewControllers.count)
        
        let width = tabBar.bounds.width / count
        
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * width, dy: 0)
        composeButton.addTarget(self, action: #selector(compose), for: .touchUpInside)
    }
}

// MARK: - 初始化子控制器
private extension WBMainTabBarController {
    func setupChildControllers() {
        let path = Bundle.main.path(forResource: "main.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        
        guard let optionArray = try? JSONSerialization.jsonObject(with: (data as Data), options: []) as? [[String:AnyObject]],
             let array = optionArray else {
            return
        }
        var childControllers = [UIViewController]()
        for dict in array {
            childControllers.append(controller(dict))
        }
        viewControllers = childControllers
    }
    
    func controller(_ dict:[String:AnyObject]) -> UIViewController {
        guard let clsName = dict["clsName"] as? String,
             let title = dict["title"] as? String,
             let imageName = dict["imageName"] as? String,
             let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? WBBaseViewController.Type,
             let visitorInfo = dict["visitorInfo"] as? [String:AnyObject] else {
                return UIViewController()
        }
        
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //访客信息字典
        vc.visitorInfo = visitorInfo
        
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
    }
}
